package controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cameraConfig.*;

public class LocationNewController extends HttpServlet {


	private static final long serialVersionUID = 1L;
	
	public void process ( HttpServletRequest request, HttpServletResponse response ) {
		ServletContext context;
		RequestDispatcher dispatcher;
		
		String error = null;
		String dispatcherTarget = "/cameraConfig.jsp";
		context = getServletContext();
		
		KameraDAO camDAO = new KameraDAO();

		if(request.getParameterNames() != null)
        {		
			if(request.getParameter("btnAddLocation") != null)
			{
				String locName = request.getParameter("locName");
				String locDescription = request.getParameter("locDescription");
					
				if(locName != null && locDescription != null)
				{
					Standort loc = new Standort();
					loc.setName(locName);
					loc.setBeschreibung(locDescription);
					camDAO.insertPlace(loc);
				}
				else 
				{
					error = "Standort konnte nicht editiert werden.";
				}
			}
        }else 
		{
			error = "Standort konnte nicht editiert werden.";
		}				
		
		if (error != null && !error.isEmpty())
			request.setAttribute("errorMessage", error);
		
		dispatcher = context.getRequestDispatcher(dispatcherTarget);
				
		try {
			dispatcher.forward(request, response);
		} 
		catch (ServletException e) {
			e.printStackTrace();
			
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
    }


	public void doGet ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
	
	public void doPost ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
}
